package com.hxzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.dto.PageDTO;
import com.hxzy.entity.TbNewbeeMallAdminUser;
import com.hxzy.entity.TbNewbeeMallCarousel;
import com.hxzy.mapper.TbNewbeeMallCarouselMapper;
import com.hxzy.service.TbNewbeeMallCarouselService;
import com.hxzy.util.SessionUtil;
import com.hxzy.vo.BootstrapTableVO;
import com.hxzy.vo.ResultEnum;
import com.hxzy.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

//开启事务自动提交
@Transactional
@Service
public class TbNewbeeMallCarouselServiceImpl implements TbNewbeeMallCarouselService {
    @Autowired
    private TbNewbeeMallCarouselMapper tbNewbeeMallCarouselMapper;

    @Override
    public BootstrapTableVO search(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNumber(),pageDTO.getPageSize());
        List<TbNewbeeMallCarousel> search = this.tbNewbeeMallCarouselMapper.search(pageDTO);

        BootstrapTableVO tableVO=new BootstrapTableVO(new PageInfo(search));

        return tableVO;
    }

    @Override
    public ResultVO addOrUpdate(TbNewbeeMallCarousel tbNewbeeMallCarousel) {
        ResultVO resultVO=null;
        tbNewbeeMallCarousel.setUpdateTime(new Date());

        TbNewbeeMallAdminUser user = SessionUtil.getUser();

        tbNewbeeMallCarousel.setUpdateUser(user.getAdminUserId());
        int count=0;
        if (tbNewbeeMallCarousel.getCarouselId()==null){
            tbNewbeeMallCarousel.setCreateTime(new Date());
            tbNewbeeMallCarousel.setCreateUser(user.getAdminUserId());
            tbNewbeeMallCarousel.setIsDeleted(new Byte("0"));

            count = this.tbNewbeeMallCarouselMapper.insert(tbNewbeeMallCarousel);
        }else {
           count=this.tbNewbeeMallCarouselMapper.updateByPrimaryKeySelective(tbNewbeeMallCarousel);
        }

        if (count>0){
           resultVO = ResultVO.success(ResultEnum.SUCDESS.getMessage());
        }else{
            resultVO=ResultVO.failed(ResultEnum.PARAMS_FAILED);
        }


        return  resultVO;
    }

    @Override
    public int insert(TbNewbeeMallCarousel record) {
       return this.tbNewbeeMallCarouselMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKeySelective(TbNewbeeMallCarousel record) {
        return this.tbNewbeeMallCarouselMapper.updateByPrimaryKeySelective(record);
    }
}
